package com.example.OnlineStorage.Controller.ShareFilePermissions;

import com.example.OnlineStorage.enums.GlobalVariables_String;
import com.example.OnlineStorage.pojo.Response.ApiResponse;
import com.example.OnlineStorage.pojo.entity.ShareFilePermissions;
import com.example.OnlineStorage.service.ShareFilePermissions.ShareFilePermissionsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/ShareFilesAdmin")
@Tag(name = "共享文件管理API")
@Slf4j
public class ShareFilePermissionsController {

    private final ShareFilePermissionsService shareFilePermissionsService;

    private final String TOKEN = GlobalVariables_String.TOKEN.getValue();

    public ShareFilePermissionsController(ShareFilePermissionsService shareFilePermissionsService) {
        this.shareFilePermissionsService = shareFilePermissionsService;
    }

    @Operation(summary = "修改共享文件夹权限",
            description = "**注意**：此API需要在请求头添加认证Token,字段名为 `Token`")
    @Parameters({
            @Parameter(name = "shareFileId", description = "共享文件夹ID", required = true),
            @Parameter(name = "userId", description = "要修改的用户", required = true),
            @Parameter(name = "permissions", description = "要修改为的权限位", required = true),
    })
    @PostMapping("/updateShareFilePermissionsByUserId")
    public ResponseEntity<Object> updateShareFilePermissionsByUserId(Long shareFileId, Long userId, Long permissions,HttpServletRequest request){
        String UUID = request.getHeader(TOKEN);
        shareFilePermissionsService.updateShareFilePermissionsByUserId(shareFileId,userId,permissions,UUID);
        return ApiResponse.GetApiResponse().ok().body("修改成功.").build();
    }

    @Operation(summary = "根据共享id查询该共享的所有成员权限信息",
            description = "**注意**：此API需要在请求头添加认证Token,字段名为 `Token`")
    @Parameters({
            @Parameter(name = "shareFileId", description = "共享文件夹ID", required = true),
            @Parameter(name = "page", description = "第几页", required = true),
    })
    @GetMapping("/getShareFilePermissionsByShaFileId")
    public ResponseEntity<Object> getShareFilePermissionsByShaFileId (Long shareFileId, Integer page){
        return ApiResponse.GetApiResponse()
                .ok()
                .body(shareFilePermissionsService.getShareFilePermissionsByShaFileId(shareFileId,page))
                .build();
    }

    @Operation(summary = "查询一个共享文件夹有多少个成员",
            description = "**注意**：此API需要在请求头添加认证Token,字段名为 `Token`")
    @Parameters({
            @Parameter(name = "shareFileId", description = "共享文件夹ID", required = true)
    })
    @GetMapping("/getShareFilePermissionsCountByShareFileId")
    public ResponseEntity<Object> getShareFilePermissionsCountByShareFileId(Long shareFileId){
        return ApiResponse.GetApiResponse()
                .ok()
                .body(shareFilePermissionsService.getShareFilePermissionsCountByShareFileId(shareFileId))
                .build();
    }

    @Operation(summary = "查询用户加入的共享文件夹列表",
            description = "**注意**：此API需要在请求头添加认证Token,字段名为 `Token`")
    @Parameters({
            @Parameter(name = "page", description = "第几页", required = true),
    })
    @GetMapping("/getShareFilePermissionsByUserId")
    public ResponseEntity<Object> getShareFilePermissionsByUserId(Integer page,HttpServletRequest request){
        String UUID = request.getHeader(TOKEN);
        return ApiResponse.GetApiResponse()
                .ok()
                .body(shareFilePermissionsService.getShareFilePermissionsByUserId(page,UUID))
                .build();
    }

    @Operation(summary = "搜索共享文件夹",
            description = "**注意**：此API需要在请求头添加认证Token,字段名为 `Token`")
    @Parameters({
            @Parameter(name = "name", description = "搜索关键字", required = true),
    })
    @GetMapping("/selectByName")
    public ResponseEntity<Object> selectByName(String name){
        return ApiResponse.GetApiResponse()
                .ok()
                .body(shareFilePermissionsService.selectByName(name))
                .build();
    }

    @Operation(summary = "加入一个共享文件夹",
            description = "**注意**：此API需要在请求头添加认证Token,字段名为 `Token`")
    @Parameters({
            @Parameter(name = "shareFileId", description = "共享文件夹ID", required = true),
    })
    @PostMapping("/joinShareFiles")
    public ResponseEntity<Object> joinShareFiles(Long shareFileId,HttpServletRequest request){
        String UUID = request.getHeader(TOKEN);
        shareFilePermissionsService.joinShareFiles(shareFileId,UUID);
        return ApiResponse.GetApiResponse()
                .ok()
                .body("加入成功.")
                .build();
    }

    @Operation(summary = "获取共享文件夹的文件信息",
            description = "**注意**：此API需要在请求头添加认证Token,字段名为 `Token`")
    @Parameters({
            @Parameter(name = "shareFileId", description = "共享文件夹ID", required = true),
    })
    @GetMapping("/getShareFilesFileDataJson")
    public ResponseEntity<Object> getShareFilesFileDataJson(Long shareFileId,HttpServletRequest request){
        String UUID = request.getHeader(TOKEN);
        return ApiResponse.GetApiResponse()
                .ok()
                .body(shareFilePermissionsService.getShareFilesFileDataJson(shareFileId,UUID))
                .build();
    }

}
